<template>
	<view >
		<!-- 收货地址 -->
		<view class="pg-30 addBox">
			<view class="font-32">收货信息</view>
			<navigator v-if="!useraddress.length" url="/goodsPackage/addressList/addressList" hover-class="none" class="mt-30 addBtn">+添加新地址</navigator>
			<navigator v-else url="/goodsPackage/addressList/addressList" hover-class="hover-class"  class="mt-30 pg-30 addData font-28">
				<view class="d-flex-yCenter-sb">
					<text>{{addressData.uname}}</text>
					<u-icon name="arrow-right" color="#000" size="28"></u-icon>
				</view>
				<view class="mt-10">{{addressData.phone}}</view>
				<view class="mt-10" style="width: 85%;">{{addressData.address}}</view>
			</navigator>
		</view>
		<template v-if="orderData.total_integral!=undefined">
			<!-- 商品信息 -->
			<view class="border-b pg-30">
				<view class="d-flex-yCenter-sb">
					<text class="font-32">订单详情</text>
					<text class="font-26 textColor999">{{orderData.shop_list[0].goods_list.length}}件商品</text>
				</view>
				<!-- 商品列表 -->
				<order-goodsList integral :goodsType='goodsType' :goodsList='orderData.shop_list[0].goods_list'></order-goodsList>
			</view>
			<!-- 配置信息 -->
			<view class="px-30">
				<!-- <picker @change="Placement_method"  :range="['UU跑腿']">
				   <x-card-strip leftText="配送方式" rightText='UU跑腿'></x-card-strip>        
				</picker> -->
				 <x-card-strip leftText="配送方式" rightText='快递配送'></x-card-strip>        
				<!-- <x-card-strip @cardEvent='Coupon_choice' leftText="优惠劵" :rightText='couponText' :rightTextColor="orderData.coupon_list.length?'#000':'#000'"></x-card-strip> -->
				 <picker @change="bindPickerChange"  range-key='city_name' :range="otherwarehouse_lst">
				     <x-card-strip  leftText="发货地址"  :rightText='otherwarehouse' ></x-card-strip>            
				 </picker>
				<!-- <x-card-strip leftText="发票" rightText='仅提供电子发票,如需纸制发票请联系客服' rightTextColor="#ddd" ></x-card-strip> -->
			</view>
			<!-- 商品结算信息 -->
			<view class="pg-30" style="margin-bottom: 170rpx;">
				<!-- <view class="d-flex-yCenter-sb">
					<view class="d-flex aling-centen">
						<text>小计<text class="ml-20 font-24 textColor999">1件商品</text></text>
					</view>
					<text class="Price-color">￥9</text>
				</view> -->
				
				<view class="mt-10 d-flex-yCenter-sb">
					<text>小计</text>
					<text class="Price-color">{{orderData.total_integral}}积分+￥{{orderData.total_price_selling}}</text>
				</view>
				<view class="mt-10 d-flex-yCenter-sb">
					<text>配送费</text>
					<text class="Price-color">￥{{orderData.shop_list[0].express.express_price}}</text>
				</view>
				<!-- <view class="mt-10 d-flex-yCenter-sb">
					<text>优惠金额</text>
					<text class="Price-color">￥{{orderData.discount_money}}</text>
				</view> -->
			</view>
			<!-- 底部导航 -->
			<view class="position-flex-bottom px-30 box-shadow-g d-flex-yCenter-sb" >
				<view class="font-28">
					<text>合计: </text>
					<text class="Price-color">{{orderData.total_integral}}积分+￥{{orderData.total_price_selling}}</text>
				</view>
				<view class="btn" @tap='submit'>提交订单</view>
			</view>
		</template>
		<!-- 支付失败弹出 -->
		<wybPopup
			ref="popup" 
			type="bottom" 
			width="500" 
			radius="6" 
			:maskClickClose='false'
			:showCloseIcon="true"
		>
			<view class="pg-30">
				<view class="font-30 mt-60 font-w" >支付失败</view>
				<view class="font-26 mt-20">
					很抱歉，支付失败。请尽快完成支付，否则订单将被取消
				</view>
				<view class="mt-60">
					<x-button title="重新支付" @btnClick='Repayments'></x-button>
				</view>
				<view class="mt-30">
					<x-button title="取消" btnStatus="2" @btnClick='cancel'></x-button>
				</view>
			</view>
		</wybPopup>
		<!-- <orderCoupon ref='order_coupon' @use-coupon='useCoupon' :list='orderData.coupon_list'></orderCoupon> -->
	</view>
</template>

<script>
	import {
		http_otherwarehouse_lst,
		http_userAddress_lst,
		http_user_integral_order_confirm,
		http_user_integral_order_create
	} from '@/config/apiRouter.js'
	/**
	 * 弹出层/选择规格
	 * 实例方法方法
	 * this.$refs.popup.hide() // 关闭
	 * this.$refs.popup.show() // 显示
	 * */
	import wybPopup from '@/components/wyb-popup/wyb-popup.vue'
	// 优惠券选择
	// import orderCoupon from '../components/order_coupon.vue'
	export default {
		components:{
			wybPopup,
		},
		data() {
			return {
				goodsType: 0, // 0 普通商品 1拼团商品，2秒杀商品
				goodsProps:{},
				otherwarehouse:'请选选择发货地址',// 发货地址
				otherwarehouse_id:null,//发货地址id
				useraddress:[],// 收货地址
				coupon_id:null,//优惠劵id
				address_id:null, // 收货地址
				addressData:{
					uname:'',
					phone:'',
					address:'',
				},
				couponText:'',
				couponText1:'',
				otherwarehouse_lst:[],// 发货地址列表
				orderData:{
					total_price:0,
					discount_money:0,
					coupon_list:[],
					shop_list:[
						{
							goods_list:[],
							express:{need_paymoney:0,express_price:0}
						}
					]
				},
				pay_log_id:null
			}
		},
		computed:{
			// coupon(){
			// 	if(this.goodsType>0)return this.couponText='暂无可用优惠券'
			// 	if(this.coupon_id!=null){
			// 		return this.couponText=this.couponText1
			// 	}
			// 	if(this.orderData.coupon_list.length) return this.couponText=this.orderData.coupon_list.length+'张优惠券可用'
			// 	return this.couponText='暂无可用优惠券'
			// },
			// 商品总价 不包含优惠劵和快递费
			goodsSun(){
				let sun=0
				this.orderData.shop_list[0].goods_list.map(item=>{
					sun+=parseFloat(item[this.goodsType==0?'price_selling':'price_seckill'])
				})
				return sun
			}
		},
		onShow() {
			// 监听修改收货地址
			uni.$on('update-address',res=>{
				if(res.id==this.address_id)return
				this.handleAdd(res)
				this.getGoodsData()
			})
			
			
		},
		
		onLoad(opens) {
			let data=JSON.parse(opens.data)
			this.goodsType=data.type
			this.goodsProps=data
			this.getUseraddress()
		},
		methods: {
			// 选择优惠券
			useCoupon(e){
				this.coupon_id=e.id
				this.getGoodsData()
				e.money_type==1?this.couponText1='抵扣'+e.money+'元':this.couponText1='打'+e.min_amount+'折'
			},
			// 提交订单（创建订单）
			submit(){
				if(!this.address_id) {this.$tool.showToast('请选择收货地址');return}
				let data=this.goodsProps
				data.address_id=this.address_id
				// data.coupon_id=this.coupon_id // 优惠劵
				data.warehouse_id=this.otherwarehouse_id //发货地址
				data.pay_type=1// 支付方式 1微信 2余额
				// data.type=this.goodsType
				http_user_integral_order_create({
					data
				}).then(res=>{
					this.pay_log_id=res.data.pay_log_id
					if(res.data.pay_log_id==0){
						this.$tool.showToast('支付成功',true)
						setTimeout(()=>{
							uni.redirectTo({
								url:'/myIntegrateShoop/my-order/my-order'
							})
						},1000)
						return
					}
					// 支付
					res.data.pay_type==1?this.Payment():this.integralPay()
					// if(res.data.pay_type==1){ // 微信支付
					// 	this.Payment()
					// }else{ // 积分支付 预留
					// 	this.integralPay
					// }
				})
			},
			// 重新支付
			Repayments(){
				if(this.pay_log_id==null){
					this.$tool.showToast('没有支付id')
					return
				}
				this.$refs.popup.hide()
				this.Payment()
			},
			// 取消
			cancel(){
				this.$refs.popup.hide()
				// uni.redirectTo({
				// 	url:'/goodsPackage/my-order/my-order?type=1'
				// })
			},
			// 积分支付
			integralPay(){
				this.PointsPayment({
					data:{
						pay_log_id:this.pay_log_id
					},
					success:(res)=>{
						// 积分商城兑换商品，当扣除积分如果还要付款，进行付款
						if (res.data.need_pay) {
							this.pay_log_id=res.data.pay_log_id
							this.Payment()
							return
						}
						this.$tool.showToast('支付成功',true)
						setTimeout(()=>{
							uni.redirectTo({
								url:'/myIntegrateShoop/my-order/my-order?type=2'
							})
						},1500)
					},
					fail:(err)=>{
						uni.showToast({
							title:err.msg,
							icon:'error',
							duration:2000
						})
						// setTimeout(()=>{
						// 	uni.redirectTo({
						// 		url:'/myIntegrateShoop/my-order/my-order?type=1'
						// 	})
						// },2000)
					}
				})
			},
			// 微信支付
			Payment(){
				this.wxPayment({
					pay_id_status:true,
					pay_log_id:this.pay_log_id,
					fail:err=>{
						this.$refs.popup.show()
					}
				}).then(res=>{
					this.$tool.showToast('支付成功',true)
					setTimeout(()=>{
						uni.redirectTo({
							url:'/myIntegrateShoop/my-order/my-order?type=2'
						})
					},1000)
				})
			},
			// 获取订单数据
			getGoodsData(){
				let data=this.goodsProps
				data.address_id=this.address_id
				// data.coupon_id=this.coupon_id
				data.warehouse_id=this.otherwarehouse_id
				console.log(data);
				http_user_integral_order_confirm({
					data
				}).then(res=>{
					this.orderData=res.data
					// if(this.goodsType>0 && this.orderData.shop_list[0].goods_list[0].state!=0){
					// 	if(this.orderData.shop_list[0].goods_list[0].state==1){
					// 		uni.showModal({
					// 			title:'提示',
					// 			content:'该商品已超过可售数量，继续购买将按原价结算',
					// 			showCancel:false
					// 		})
					// 		return
					// 	}
					// 	uni.showModal({
					// 		title:'提示',
					// 		content:'该商品已超过限购，继续购买将按原价结算',
					// 		showCancel:false
					// 	})
					// }
					console.log(res);
					
				})
			},
			// 获取用户收货地址
			getUseraddress(){
				http_userAddress_lst({
					data:{
						limit:20
					},
					complete:res=>{
						// 获取发货地址
						this.getOtherwarehouselst()
					}
				}).then(res=>{
					if(!res.data.data.length){
						uni.navigateTo({
							url:'/goodsPackage/addressList/addressList'
						})
						return
					}
					this.useraddress=res.data.data
					res.data.data.map(item=>{
						if(item.is_default==1){
							this.handleAdd(item)
							return 
						}
					})
				})
			},
			// 处理收货地址地址
			handleAdd(item){
				let addressData={
					uname:item.name,
					phone:item.phone.slice(0,3)+'****'+item.phone.slice(-4),
					address:'',
				}
				this.address_id=item.id
				addressData.address+=(item.province+item.city+item.district+item.address+item.user_note)
				this.addressData=addressData
			},
			// 获取商家发货地址
			getOtherwarehouselst(){
				http_otherwarehouse_lst().then(res=>{
					this.otherwarehouse_lst=res.data
					this.otherwarehouse_id=res.data[0].id
					this.otherwarehouse=res.data[0].city_name
					if(this.address_id==null)return
					this.getGoodsData()
				})
			},
			// 优惠选择
			// Coupon_choice(){
			// 	if(this.goodsType>0)return 
			// 	if(!this.orderData.coupon_list.length) return 
			// 	this.$refs.order_coupon.show()
			// },
			// 选择发货地址
			bindPickerChange(e){
				let index=e.detail.value
				if(this.otherwarehouse_id==this.otherwarehouse_lst[index].id)return
				this.otherwarehouse_id=this.otherwarehouse_lst[index].id
				this.otherwarehouse=this.otherwarehouse_lst[index].city_name
				this.getGoodsData()
			},
			// 配售方式
			// Placement_method(e){
			// 	let index=e.detail.value
			// 	console.log(e);
			// }
		}
	}
</script>

<style lang="scss">
	.addBox{
		.addBtn{
			width: 690rpx;
			line-height: 60rpx;
			text-align: center;
			font-size: 26rpx;
			background: #FFFFFF;
			border: 1px solid #828282;
			border-radius: 8rpx;
		}
		.addData{
			
			border: 1px solid #828282;
			border-radius: 8px;
		}
	}
	.position-flex-bottom{
		background: #fff;
		padding-top: 15rpx;
		padding-bottom: 15rpx;
		.btn{
			width: 300rpx;
			line-height: 70rpx;
			text-align: center;
			color: #fff;
			background: $color-zt;
			border-radius: 12rpx;
		}
	}
</style>
